# generate benchmarks for documentation
function generate_docbenchmark()
    a = tensor_dict[(3, 1, Float64)]
    A = tensor_dict[(3, 2, Float64)]
    As = symtensor_dict[(3, 2, Float64)]
    AA = tensor_dict[(3, 4, Float64)]
    AAs = symtensor_dict[(3, 4, Float64)]

    # arrays and voigt forms
    b   = Array(a)
    B   = Array(A);   Bv = reshape(B, (9, ))
    Bs  = Array(As);  Bsv = reshape(Bs, (9, ))
    BB  = Array(AA);  BBv = reshape(BB, (9, 9))
    BBs = Array(AAs); BBsv = reshape(BBs, (9, 9))

    open(joinpath(dirname(@__FILE__), "../docs/src/benchmarks.md"), "w") do file
        function printheader(head)
            println(file, "| **$(head)** | | | |")
        end
        function printrow(op)
            pretty = (t) -> BenchmarkTools.prettytime(BenchmarkTools.time(minimum(t)))
            speedup = (ta, tt) -> round(10*BenchmarkTools.time(minimum(ta))/BenchmarkTools.time(minimum(tt)))/10
            println(file, "| `$(op)` | $(pretty(tt)) | $(pretty(ta)) | ×$(speedup(ta, tt)) |")
        end

        print(file, """
                    # Benchmarks

                    Here are some benchmark timings for tensors in 3 dimensions. For comparison
                    the timings for the same operations using standard Julia `Array`s are also
                    presented.

                    In the table below, `a` denotes a vector, `A`, `As` denotes second order
                    non-symmetric and symmetric tensors and `AA`, `AAs` denotes fourth order
                    non-symmetric and symmetric tensors respectively.

                    | Operation  | `Tensor` | `Array` | speed-up |
                    |:-----------|---------:|--------:|---------:|
                    """)

        printheader("Single contraction")
        tt = @benchmark $a ⋅ $a
        ta = @benchmark $b ⋅ $b
        printrow("a ⋅ a")

        tt = @benchmark $A ⋅ $a
        ta = @benchmark $B * $b
        printrow("A ⋅ a")

        tt = @benchmark $A ⋅ $A
        ta = @benchmark $B * $B
        printrow("A ⋅ A")

        tt = @benchmark $As ⋅ $As
        ta = @benchmark $Bs * $Bs
        printrow("As ⋅ As")

        printheader("Double contraction")
        tt = @benchmark $A ⊡ $A
        ta = @benchmark $B ⋅ $B
        printrow("A ⊡ A")

        tt = @benchmark $As ⊡ $As
        ta = @benchmark $Bsv ⋅ $Bsv
        printrow("As ⊡ As")

        tt = @benchmark $AA ⊡ $A
        ta = @benchmark $BBv * $Bv
        printrow("AA ⊡ A")

        tt = @benchmark $AA ⊡ $AA
        ta = @benchmark $BBv * $BBv
        printrow("AA ⊡ AA")

        tt = @benchmark $AAs ⊡ $AAs
        ta = @benchmark $BBsv * $BBsv
        printrow("AAs ⊡ AAs")

        tt = @benchmark $As ⊡ $AAs ⊡ $As
        ta = @benchmark $Bsv ⋅ ($BBsv * $Bsv)
        printrow("As ⊡ AAs ⊡ As")

        printheader("Outer product")
        tt = @benchmark $a ⊗ $a
        ta = @benchmark $b * $(b)'
        printrow("a ⊗ a")

        tt = @benchmark $A ⊗ $A
        ta = @benchmark $Bv * $(Bv)'
        printrow("A ⊗ A")

        tt = @benchmark $As ⊗ $As
        ta = @benchmark $Bsv * $(Bsv)'
        printrow("As ⊗ As")

        printheader("Other operations")
        tt = @benchmark det($A)
        ta = @benchmark det($B)
        printrow("det(A)")

        tt = @benchmark det($As)
        ta = @benchmark det($Bs)
        printrow("det(As)")

        tt = @benchmark inv($A)
        ta = @benchmark inv($B)
        printrow("inv(A)")

        tt = @benchmark inv($As)
        ta = @benchmark inv($Bs)
        printrow("inv(As)")

        tt = @benchmark norm($a)
        ta = @benchmark norm($b)
        printrow("norm(a)")

        tt = @benchmark norm($A)
        ta = @benchmark norm($(Bv))
        printrow("norm(A)")

        tt = @benchmark norm($As)
        ta = @benchmark norm($(Bsv))
        printrow("norm(As)")

        tt = @benchmark norm($AA)
        ta = @benchmark norm($(BBv[:]))
        printrow("norm(AA)")

        tt = @benchmark norm($AAs)
        ta = @benchmark norm($(BBsv[:]))
        printrow("norm(AAs)")

        tt = @benchmark $a × $a
        ta = @benchmark $b × $b
        printrow("a × a")

        # print versioninfo
        println(file, """


                      The benchmarks are generated by
                      [`benchmark_doc.jl`](https://github.com/KristofferC/Tensors.jl/blob/master/benchmark/benchmark_doc.jl)
                      on the following system:

                      ```
                      julia> versioninfo()
                      """)
        versioninfo(file)
        println(file, "```")
    end
end
